package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.pojo.Bumenguanli;
import com.qf.pojo.Menu;
import com.qf.pojo.Permsetting;
import com.qf.pojo.Usermanager;
import com.qf.service.DeptService;
import com.qf.service.MenuService;
import com.qf.service.PermsettingService;
import com.qf.service.UsermanagerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/11/23 0023
 * Author->DT
 * qmxc
 * 行到水穷处 坐看云起时
 * 模块：职位/角色Controller层
 */
@RequestMapping("/role")
@Controller
public class RoleController {
    @Autowired
    private PermsettingService permsettingService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private UsermanagerService usermanagerService;
    //跳转到职位设置页面(信息展示页面)
    @RequestMapping("/list")
    @RequiresPermissions("role:list")
    public String roleListSkip(@RequestParam(defaultValue = "1")Integer index,
                               @RequestParam(defaultValue = "3")Integer page, Model model,
                               String roleName,String deptName,Integer used){

        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", page);
        map.put("roleName", roleName);
        map.put("deptName", deptName);
        map.put("used", used);
        //1.查询
        PageInfo<Permsetting> pageInfo = permsettingService.findAllRoleInfoByLimit(map);
        //2.将查询到的信息放入Request域中
        //2.1pageInfo对象
        model.addAttribute("pageInfo", pageInfo);
        //2,2数据回显信息
        model.addAttribute("roleName", roleName);
        model.addAttribute("deptName", deptName);
        model.addAttribute("used", used);

        return "resource/demo3/list";
    }

    //跳转到职位添加页面
    @RequestMapping("/add-ui")
    public String roleAddSkip(Model model){
        //查询全部部门信息
        List<Bumenguanli> deptList = deptService.findAll();
        //查询全部菜单信息
        List<Menu> menuList= menuService.findAll();
        //将查询到的信息放入Request域中
        model.addAttribute("deptList", deptList);
        model.addAttribute("menuList", menuList);
        //返回
        return "resource/demo3/add";
    }


    //执行职位信息添加
    @RequestMapping("/add")
    public void add(Permsetting role, Integer[] mids, HttpServletResponse response){
        //执行添加(添加角色信息，和角色菜单权限信息)
        Integer count=permsettingService.add(role,mids);
        //返回
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (count>=1){
            //添加成功
            writer.print("<script>alert('添加成功');location.href='/role/list'</script>");
        }else {
            //添加失败
            writer.print("<script>alert('添加失败，请重新尝试！');location.href='/role/add-ui'</script>");
        }
    }

    //跳转到更新角色列表
    @RequestMapping("/update-ui")
    public String updateUI(Integer rid,Model model){
        //1.根据id查询该角色信息
        Permsetting role = permsettingService.selectByPrimaryKey(rid);
        //2.查询全部部门信息
        List<Bumenguanli> deptList = deptService.findAll();
        //3.查询全部菜单信息
        List<Menu> menuList = menuService.findAll();
        //将页面需要的信息放到请求域中
        model.addAttribute("role", role);
        model.addAttribute("deptList", deptList);
        model.addAttribute("menuList" ,menuList);

        //返回
        return "resource/demo3/update";

    }

    //更新角色和角色菜单表
    @RequestMapping("/update")
    public void updateUI(Permsetting role,Integer[] mids,HttpServletResponse response){
        //修改角色信息
        permsettingService.updateByPrimaryKeySelective(role);
        //修改角色菜单表信息(先删除原来该角色对应的菜单，然后再增加新的菜单信息)
        Integer count = menuService.updateRoleMenuByRoleId(role.getId(), mids);
        //判断修改是否成功，响应用户
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (count>=1){
            //添加成功
            writer.print("<script>alert('修改成功');location.href='/role/list'</script>");
        }else {
            //添加失败
            writer.print("<script>alert('修改失败，请重新尝试！');location.href='/role/update-ui'</script>");
        }
    }

    //单行角色删除(根据角色id删除)
    @RequestMapping("/delete-one")
    public void deleteRoleByRid(Integer rid,HttpServletResponse response){
        //判断角色是否被占用
        List<Usermanager> list = usermanagerService.findByRoleId(rid);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (list.size()!=0){
            //角色被占用
            writer.print("<script>alert('删除失败,该角色已被用户占用！');location.href='/role/list'</script>");
        }
        if (list.size()==0){
            //角色未被占用，可执行删除
            //删除角色菜单数据表中该角色id对应的信息
            Integer integer = menuService.deleteRoleMenuByRoleId(rid);
            //删除角色表
            Integer count = menuService.deleteRoleByRoleId(rid);
            if (count>=1){
                //删除成功
                writer.print("<script>alert('删除成功');location.href='/role/list'</script>");
            }else {
                //删除失败
                writer.print("<script>alert('删除失败，请重新尝试！');location.href='/role/list'</script>");
            }
        }
    }

    //多行角色删除(逻辑删除复选框选择的所有选择的角色)+导出数据
    @RequestMapping("/delete-checked")
    public void deleteChecked(Integer[] rid,HttpServletResponse response,String method) throws IOException {
        //method相当于鉴别器，根据传入的method不同，进行不同处理
        //构建响应流，给用户提示，返回类型必需为void
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;

        //调用Service执行批量删除
        if (method.equals("delete")) {
            for (Integer integer : rid) {
                if (integer==11||integer==12){
                    writer = response.getWriter();
                    writer.print("<script>alert('你没有权限删除管理员和领导账户！');location.href='/role/list'</script>");
                    return;
                }
            }
            int count = usermanagerService.updateCheckedUesd(rid);
            //判断删除是否成功
            try {
                writer = response.getWriter();
                if (count >= 1) {
                    //删除成功
                    writer.print("<script>alert('删除成功');location.href='/role/list'</script>");

                } else {
                    //删除失败
                    writer.print("<script>alert('删除失败');location.href='/role/list'</script>");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    }
